package com.wkh.project.driver.service.impl;

import cn.hutool.core.util.StrUtil;
import com.wkh.project.common.result.Result;
import com.wkh.project.common.result.ResultCodeEnum;
import com.wkh.project.dispatch.client.NewOrderFeignClient;
import com.wkh.project.driver.client.DriverInfoFeignClient;
import com.wkh.project.driver.service.DriverService;
import com.wkh.project.map.client.LocationFeignClient;
import com.wkh.project.model.form.driver.DriverFaceModelForm;
import com.wkh.project.model.form.driver.UpdateDriverAuthInfoForm;
import com.wkh.project.model.vo.driver.DriverAuthInfoVo;
import com.wkh.project.model.vo.driver.DriverLoginVo;
import com.wkh.project.service.constant.RedisConstant;
import com.wkh.project.service.execption.GuiguException;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {
    @Resource
    private DriverInfoFeignClient driverInfoFeignClient;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private NewOrderFeignClient newOrderFeignClient;

    @Resource
    private LocationFeignClient locationFeignClient;

    @SneakyThrows
    @Override
    public String login(String code) {
        //获取openId
        Long driverId = driverInfoFeignClient.login(code).getData();

        String token = StrUtil.uuid().replaceAll("-", "");
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token, driverId.toString(), RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
        return token;
    }

    @Override
    public DriverLoginVo getDriverLoginInfo(Long driverId) {
        return driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
    }

    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        Result<DriverAuthInfoVo> authInfoVoResult = driverInfoFeignClient.getDriverAuthInfo(driverId);
        DriverAuthInfoVo driverAuthInfoVo = authInfoVoResult.getData();
        return driverAuthInfoVo;
    }

    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        Result<Boolean> booleanResult = driverInfoFeignClient.UpdateDriverAuthInfo(updateDriverAuthInfoForm);
        return booleanResult.getData();
    }

    //创建司机人脸模型
    @Override
    public Boolean creatDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> booleanResult = driverInfoFeignClient.creatDriverFaceModel(driverFaceModelForm);
        if (booleanResult.getCode()!=200){
            throw new GuiguException(ResultCodeEnum.SERVICE_ERROR);
        }
        return booleanResult.getData();
    }

    @Override
    public Boolean isFaceRecognition(Long driverId) {
        Result<Boolean> faceRecognition = driverInfoFeignClient.isFaceRecognition(driverId);
        Integer code = faceRecognition.getCode();
        if (code != 200){
            throw new GuiguException(ResultCodeEnum.SERVICE_ERROR);
        }
        return faceRecognition.getData();
    }

    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        Result<Boolean> booleanResult = driverInfoFeignClient.verifyDriverFace(driverFaceModelForm);
        Integer code = booleanResult.getCode();
        if (code != 200){
            throw new GuiguException(ResultCodeEnum.FACE_AUTHENTICATION_FAILED);
        }
        Boolean result = booleanResult.getData();
        return result;
    }

    @Override
    public Boolean startService(Long driverId) {
        //判断认证状态
        DriverLoginVo driverLoginVo = driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
        if(driverLoginVo.getAuthStatus() != 2) {
            throw new GuiguException(ResultCodeEnum.AUTH_ERROR);
        }
        //判断当日是否人脸识别
        Boolean isFaceRecognition = driverInfoFeignClient.isFaceRecognition(driverId).getData();
        if(!isFaceRecognition) {
            throw new GuiguException(ResultCodeEnum.FACE_AUTHENTICATION_FAILED);
        }
        try {
            //更新司机接单状态
            driverInfoFeignClient.updateServiceStatus(driverId, 1);
            //删除司机位置信息
            locationFeignClient.removeDriverLocation(driverId);
            //清空司机新订单队列
            newOrderFeignClient.clearNewOrderQueueData(driverId);
        } catch (Exception e) {
            throw new GuiguException(ResultCodeEnum.SYSTEM_ERROR);
        }
        return true;
    }

    @Override
    public Boolean stopService(Long driverId) {
        //更新司机接单状态
        driverInfoFeignClient.updateServiceStatus(driverId, 0);

        //删除司机位置信息
        locationFeignClient.removeDriverLocation(driverId);

        //清空司机新订单队列
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return null;
    }
}
